package com.imooc.imoocbilibili.controller;

import java.util.Arrays;
import java.util.Map;

import com.imooc.imoocbilibili.interceptor.ThreadUser;
import com.imooc.imoocbilibili.utils.RSAUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.imooc.imoocbilibili.entity.User;
import com.imooc.imoocbilibili.service.UserService;
import com.imooc.imoocbilibili.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户表
 *
 * @author jl
 * @since 2022-04-10 21:13:43
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 获取公钥
     */
    @GetMapping("/rsa-pks")
    public R<String> getRsaPulicKey(){
        String pk = RSAUtil.getPublicKeyStr();
        return R.ok(pk);
    }

    /**
     * 注册
     */
    @PostMapping("/users")
    public R<String> addUser(@RequestBody User user){
        userService.addUser(user);
        return R.ok();
    }

    /**
     * 登录
     */
    @PostMapping("/user-tokens")
    public R<String> login(@RequestBody User user) throws Exception{
        String token = userService.login(user);
        return R.ok(token);
    }

    /**
     * 获取当前登录用户
     */
    @GetMapping("/users")
    public R<User> getUserInfo(){
        User user = ThreadUser.getUser();
        return R.ok(user);
    }

    @PutMapping("/users")
    public R<String> updateUsers(@RequestBody User user) {
        Long userId = ThreadUser.getUser().getId();
        user.setId(userId);
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 分页查询用户表
     */
    @GetMapping("/user-infos")
    public R list(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "nick", required = false) String nick) {
        return R.ok(userService.queryPage(pageNo, pageSize, nick));
    }

    /**
     * 返回双token
     */
    @PostMapping("/user-dts")
    public R loginForDts(@RequestBody User user) throws Exception {
        Map<String, Object> map = userService.loginForDts(user);
        return R.ok(map);
    }

    /**
     * 退出删除refreshToken
     */
    @DeleteMapping("/refresh-tokens")
    public R logout() {
        userService.logout();
        return R.ok();
    }

    @PostMapping("access_tokens")
    public R refreshAccessTokens(HttpServletRequest request) throws Exception {
        String refreshToken = request.getHeader("refreshToken");
        Map<String, Object> map = userService.refreshAccessTokens(refreshToken);
        return R.ok(map);
    }

    /**
     * 根据主键查询用户表
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        User user =userService.getById(id);
        return R.ok(user);
    }

    /**
     * 修改用户表
     */
    @PutMapping("/update")
    public R update(@RequestBody User user) {
            userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除用户表
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
            userService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
